import requests
import parsel


for page in range(1,3):

    url = f'https://b.faloo.com/1163585_{page}.html'

    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
    }

    reponse = requests.get(url=url, headers=headers)
    print(reponse.text)
    selector = parsel.Selector(reponse.text)
    #css选择器，就是根据标签的属性提取数据
    title = selector.css('.c_l_title h1::text').get().replace('偷吃我外卖被辣哭，问我要索赔？  ' ,'') #获取一个标签内容 返回字符串
    content_list = selector.css('div.noveContent p::text').getall() #获取所有标签内容，返回数据列表
    content = '\n'.join(content_list) #将list列表使用换行符连接起来

    with open('偷吃我外卖' + '.txt',mode='a',encoding='utf-8') as f:
        f.write(title)
        f.write('\n')
        f.write(content)
        f.write('\n')